using Echo.Context.UltiProNet.Contexts;
using Echo.Context.UltiProNet.Macros.Database.PayrollHelpers;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.TestSupport.Service;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Dionny Santiago
////Date Created: October 23, 2012
////Project: ASP.NET Payroll Site Admin Smoke Test
////*************************************************************************************************************************************

namespace Echo.Automation.Payroll
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Dionny Santiago", "dionny_santiago@ultimatesoftware.com")]
    [Category("BADSmokeTest")]
    [Category("Payroll Processing")]
    [Timeout(2400)]
    public class OLDPayrollSiteAdmin : AutomatedTest
    {
        const string paygroup = "APTOWK";
        const string paygroup2 = "AUPTO2";
        const string loginUserName = "susann";
        const string clientID = "SB11";
        const int payrollModelID = 500004;
        const string templateCode = "5Q8UEO0000K0";

        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        PayrollTearDown APTOWK;
        PayrollTearDown AUPTO2;

        [FixtureSetUp]
        public void SetUp()
        {
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_SB11");
            Database.Site = ConfigDatabase("HRMS_SITE", Environment.UltiPro.SiteServer);

            // Add rollbacks.
            Database.Company.AddRollBack("paygroup", "pgrpaygroup in ('aptomn', 'aptowk')");
            Database.Company.AddRollBack("PayGroupModel", "pgmPayGroup = 'aptowk'");

            Database.Company.Execute("update paygroup set pgrIsPayRollOpen = 'N' WHERE pgrpaygroup = 'aptomn'");
            Database.Company.Execute("UPDATE PayGroup SET PgrChargeDate = '2005-01-03' where PgrPayGroup = 'APTOWK'");
            Database.Company.Execute("update PayGroup set pgrPeriodControl = '200501031' WHERE pgrPayGroup ='aptowk'");
            Database.Company.Execute("delete from PayGroupModel where pgmPayGroup = 'aptowk'");
            Database.Company.Execute("insert into PayGroupModel (pgmPayGroup, pgmPayrollModelID, pgmPayrollType) VALUES ('APTOWK', 500002, 'R')");
            Database.Company.Execute("insert into PayGroupModel (pgmPayGroup, pgmPayrollModelID, pgmPayrollType) VALUES ('APTOWK', 500007, 'S')");

            APTOWK = Database.Macros.PayrollProcessing.PayrollSetup(paygroup, payrollModelID, templateCode, "R", clientID, loginUserName, this);
            AUPTO2 = Database.Macros.PayrollProcessing.PayrollSetup(paygroup2, payrollModelID, templateCode, "R", clientID, loginUserName, this);

            // Ensure the big three services have been stopped/started
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "UESService");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "UESService");
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "DistributedProcessManager");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "DistributedProcessManager");
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "UltimateSoftware.FileManagement.WindowsService");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "UltimateSoftware.FileManagement.WindowsService");

            // Launch the browser
            UltiPro = Launch<UltiPro>();

            UltiPro.Pages.Login.LoginAs(loginUserName, "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("SB11", "ABATC");

            UltiPro.Menu.PayrollProcessing.Click(link => link.PayrollGateway);

            // Verify page header
            UltiPro.Pages.PayrollGateway.Is(control => control.PageHeaderLabel, "innerHtml", "Payroll Gateway");

            // Expand to view the full page
            UltiPro.Pages.PayrollGateway.Click(page => page.FullPageView);

            // Verify page header
            UltiPro.Pages.ProcessPay.Is(control => control.PageHeaderLabel, "innerHtml", "Process Pay");

            // View by Status
            UltiPro.Pages.ProcessPay.Click(page => page.ViewByStatusButton);

            // Expand the Start payroll section
            UltiPro.Pages.ProcessPay.Click(page => page.StartPayrollCollapsibleSection);

            // Verify the Pay Group Process Alerts section exists
            UltiPro.Pages.ProcessPay.Exists(page => page.PayGroupProcessingAlertsCollapsibleSection);
        }

        [FixtureTearDown]
        public void TearDown()
        {
            UltiPro.Browser.Quit();

            APTOWK.TearDownAllDataWithRollback();
            AUPTO2.TearDownAllDataWithRollback();
            Database.Company.RollBackDatabase();
        }

        [Test(Order = 1)]
        public void Verify_the_user_can_see_all_paygroups()
        {
            ////*************************************************************************************************************
            ////      Test Case 1: Verify the user can see all paygroups
            ////      Test Data: susann/password , Pay Groups all in StartPayrollGrid: AUTO PTO Weekly, AUTO PTO Monthly, Tips Biweekly2
            ////      Anticipated Results: The user can see all paygroups
            ////*************************************************************************************************************

            // Verify Paygroup AUTO PTO Weekly exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").RowExists();

            // Verify Paygroup AUTO PTO Monthly exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Monthly").RowExists();

            // Verify Tips Biweekly2 exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Tips Biweekly2").RowExists();
        }

        [Test(Order = 2)]
        [Timeout(1200)]
        public void Verify_the_user_can_open_a_payroll()
        {
            ////*************************************************************************************************************
            ////      Test Case 2: Verify the user can open a payroll
            ////      Test Data: auto_pto1m/password , Pay Groups: AUTO PTO Weekly, AUTO PTO2 Bi-weekly
            ////      Anticipated Results: The user can open a payroll
            ////*************************************************************************************************************

            // Select Paygroup AUTO PTO Weekly.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Click(row => row.CheckBox);

            // Select Paygroup AUTO PTO2 Bi-weekly Monthly.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Click(row => row.CheckBox);

            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, APTOWK.PerControl, "ECF60693-40CC-4216-ADB4-118989A2DAC6");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, AUPTO2.PerControl, "ECF60693-40CC-4216-ADB4-118989A2DAC6");
            Database.Company.VerifyValue("select pgrispayrollopen from paygroup where pgrpaygroup = 'APTOWK'", "Y");
            Database.Company.VerifyValue("select pgrispayrollopen from paygroup where pgrpaygroup = 'AUPTO2'", "Y");

            // Collapse the Start payroll collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.StartPayrollCollapsibleSection);
        }

        [Test(Order = 3)]
        public void Verify_a_site_admin_can_bring_in_time()
        {
            ////*************************************************************************************************************
            ////      Test Case 3: Verify a site admin can bring in time
            ////      Test Data: auto_pto1m/password , Pay Groups: AUTO PTO Weekly, AUTO PTO2 Bi-weekly
            ////      Anticipated Results: A site admin can bring in time.
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Bring in time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.BringInTimeCollapsibleSection);

            // Bring in time for the payroll.
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, APTOWK.PerControl, "F8E6427E-4DAC-4C39-BF1D-B02E90F191F7");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, AUPTO2.PerControl, "F8E6427E-4DAC-4C39-BF1D-B02E90F191F7");
            Database.Company.VerifyRecordCount("select top 1 * From tcwkern where tcepaygroup = 'APTOWK'", 1, 60);
            Database.Company.VerifyRecordCount("select top 1 * From tcwkern where tcepaygroup = 'AUPTO2'", 1, 60);

            // Collapse the Bring in time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.BringInTimeCollapsibleSection);
        }

        [Test(Order = 4)]
        [Timeout(1200)]
        public void Verify_a_user_can_perform_verify_time()
        {
            ////*************************************************************************************************************
            ////      Test Case 4: Verify a user can perform verify time
            ////      Test Data: auto_pto1m/password , Pay Groups: AUTO PTO Weekly, AUTO PTO2 Bi-weekly
            ////      Anticipated Results: A user can perform verify time
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.VerifyTimeCollapsibleSection);

            // Verify time for the payroll.
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, APTOWK.PerControl, "8ED09192-6AE0-41DB-83C9-1118561BF56A");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, AUPTO2.PerControl, "8ED09192-6AE0-41DB-83C9-1118561BF56A");

            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.VerifyTimeCollapsibleSection);
        }

        [Test(Order = 5)]
        [Timeout(1200)]
        public void Verify_a_site_admin_can_select_Submit_Time_and_Calculate()
        {
            ////*************************************************************************************************************
            ////      Test Case 5: Verify a site admin can select Submit Time and Calculate
            ////      Test Data: auto_pto1m/password , Pay Groups: AUTO PTO Weekly, AUTO PTO2 Bi-weekly
            ////      Anticipated Results: A user can perform submit time and calculate
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.SubmitTimeAndCalculateCollapsibleSection);

            // Submit time and calculate for the payroll.
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, APTOWK.PerControl, "F99694A2-4D7B-4474-BD3C-517CF815F208");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, AUPTO2.PerControl, "F99694A2-4D7B-4474-BD3C-517CF815F208");
            Database.Company.VerifyRecordCount("select top 1 * from m_batch where mbtPayGroup = 'APTOWK'", 1, 60);
            Database.Company.VerifyRecordCount("select top 1 * from m_batch where mbtPayGroup = 'AUPTO2'", 1, 60);

            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.SubmitTimeAndCalculateCollapsibleSection);
        }

        [Test(Order = 6)]
        [Timeout(1200)]
        public void Verify_a_site_admin_can_Print_and_Finish_a_payroll()
        {
            ////*************************************************************************************************************
            ////      Test Case 6: Verify a site admin can Print and Finish a payroll
            ////      Test Data: auto_pto1m/password , Pay Groups: AUTO PTO Weekly, AUTO PTO2 Bi-weekly
            ////      Anticipated Results: A user can Print and Finish a payroll
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.PrintPayrollCollapsibleSection);

            // Print the payroll.
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO Weekly").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "AUTO PTO2 Bi-weekly").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, APTOWK.PerControl, "B14D0AB0-A51C-433C-9278-C53A44EB1C24");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, AUPTO2.PerControl, "B14D0AB0-A51C-433C-9278-C53A44EB1C24");

            // Verify database records for paygroup APTOWK
            Database.Company.VerifyRecordCount("select * from m_batch where MbtPayGroup = 'APTOWK'", 0);
            Database.Company.VerifyValue("select PgrIsPayRollOpen from paygroup where pgrpaygroup = 'APTOWK'", "N");
            Database.Company.VerifyRecordCount(string.Format("select * from paygroup where pgrpaygroup = 'APTOWK' and pgrperiodcontrol = '{0}'", APTOWK.PerControl), 0);
            Database.Company.VerifyRecordCount(string.Format("select * from paygroup where pgrpaygroup = 'APTOWK' and pgrpayperiodid = '{0}'", APTOWK.PayPeriodID), 0);

            // Verify database records for paygroup AUPTO2
            Database.Company.VerifyRecordCount("select * from m_batch where MbtPayGroup = 'AUPTO2'", 0);
            Database.Company.VerifyValue("select PgrIsPayRollOpen from paygroup where pgrpaygroup = 'AUPTO2'", "N");
            Database.Company.VerifyRecordCount(string.Format("select * from paygroup where pgrpaygroup = 'AUPTO2' and pgrperiodcontrol = '{0}'", AUPTO2.PerControl), 0);
            Database.Company.VerifyRecordCount(string.Format("select * from paygroup where pgrpaygroup = 'AUPTO2' and pgrpayperiodid = '{0}'", AUPTO2.PayPeriodID), 0);

            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.PrintPayrollCollapsibleSection);
        }
    }
}
